﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// 应用静态配置
    /// </summary>
    public static class Application
    {
        static Application() 
        {
        
        }
        /// <summary>
        /// 是否是开发环境
        /// </summary>
        public static Boolean IsDevelopment { get; set; }

        /// <summary>
        ///  应用服务
        /// </summary>
        public static IServiceCollection Services { get; set; }

        private static IServiceProvider serviceProvider = null;
        /// <summary>
        /// 服务提供器
        /// </summary>
        public static IServiceProvider ServiceProvider
        {
            get
            {
                if (serviceProvider==null)
                {
                    serviceProvider = Services.BuildServiceProvider();
                }
                return serviceProvider;
            }
        }
        /// <summary>
        /// 获取请求生命周期的服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>()where T : class
        {
            return GetService(typeof(T)) as T;
        }
        /// <summary>
        /// 获取请求生命周期的服务
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetService(Type type)
        {
            return ServiceProvider.GetService(type);
        }
        /// <summary>
        /// 获取请求生命周期的服务
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public static TService GetRequiredService<TService>()
            where TService : class
        {
            return GetRequiredService(typeof(TService)) as TService;
        }

        /// <summary>
        /// 获取请求生命周期的服务
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetRequiredService(Type type)
        {
            return ServiceProvider.GetRequiredService(type);
        }
        /// <summary>
        /// 应用运行根目录
        /// </summary>
        public static string BaseDirectory { get { return AppContext.BaseDirectory; } }
    }
}

